﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace BtLabelBLL
{
    public class UiBLL
    {
        /// <summary>
        /// 清空日志
        /// </summary>
        /// <param name="RichTextBoxInfo"></param>
        public static void RichTextBoxClearText(RichTextBox RichTextBoxInfo)
        {
            RichTextBoxInfo.Dispatcher.Invoke((Action)delegate
            {
                RichTextBoxInfo.Document.Blocks.Clear();
                RichTextBoxInfo.ScrollToVerticalOffset(0);
            });
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="RichTextBoxInfo"></param>
        /// <param name="brush"></param>
        /// <param name="txt"></param>
        public static void RichTextBoxAppendText(RichTextBox RichTextBoxInfo, System.Windows.Media.Brush brush, string txt,int size=0)
        {
            RichTextBoxInfo.Dispatcher.Invoke((Action)delegate
            {
                var p = new Paragraph(); // Paragraph 类似于 html 的 P 标签
                var r = new Run(txt); // Run 是一个 Inline 的标签
                p.Inlines.Add(r);
                p.Foreground = brush;//设置字体颜色
                if (size != 0)
                {
                    p.FontSize = size;
                }

                p.Margin = new System.Windows.Thickness(3, 5, 3, 5);
                RichTextBoxInfo.Document.Blocks.Add(p);
                //todo 验证
                if (RichTextBoxInfo.Document.Blocks.Count > 1000)
                {
                    RichTextBoxInfo.Document.Blocks.Remove(RichTextBoxInfo.Document.Blocks.FirstBlock);
                }
                //滚动到末尾
                RichTextBoxInfo.ScrollToVerticalOffset(RichTextBoxInfo.Document.Blocks.Count * 200);
            });
        }
        public static void SetTextBox(TextBox textBox, string value)
        {
            textBox.Dispatcher.Invoke(() =>
            {
                textBox.Text = value;
            });
        }
        public static void SetBorderBackColor(Border border, SolidColorBrush colorBrush)
        {
            border.Dispatcher.Invoke(() =>
            {
                border.Background = colorBrush;
            });
        }
        public static void SetImage(Image image, string path)
        {
            image.Dispatcher.Invoke(() =>
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
                bi.EndInit();
                image.Source = bi.Clone();
            });
        }
        public static void SetImageOpacity(Image image, double opacity)
        {
            image.Dispatcher.Invoke(() =>
            {
                image.Opacity = opacity;
            });
        }

    }
}
